import java.io.*;
import java.util.*;

public class FileOperations {
    public static void main(String[] args) {
        String inputFilePath = "C:\\Users\\yyx13\\Desktop\\vscod\\shiyan4\\shiyanx\\src\\prices.txt";
        String outputFilePath = "C:\\Users\\yyx13\\Desktop\\numbered_prices.txt";

        addLineNumbers(inputFilePath, outputFilePath);
        calculateAndAppendAverage(inputFilePath);
    }

    // 添加行号到文件
    public static void addLineNumbers(String inputFile, String outputFile) {
        try (Scanner scanner = new Scanner(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"));
             PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"))) {
            int lineNumber = 1;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                writer.printf("%d: %s%n", lineNumber++, line);
            }
        } catch (FileNotFoundException e) {
            System.err.println("输入文件未找到: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("写入文件错误: " + e.getMessage());
        }
    }

    // 计算并追加平均价格
    public static void calculateAndAppendAverage(String filePath) {
        File file = new File(filePath);
        Scanner sc = null;
        int count = 0;
        double sum = 0;

        try {
            sc = new Scanner(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            sc.useDelimiter("[^0-9.-]+"); // 只提取数字及小数点、负号

            while (sc.hasNext()) {
                if (sc.hasNextDouble()) {
                    double price = sc.nextDouble();
                    count++;
                    sum += price;
                    System.out.println("读取价格: " + price);
                } else {
                    // 输出当前读取的非数字部分，方便调试
                    System.out.println("跳过非数字部分: " + sc.next());
                }
            }

            if (count > 0) {
                double average = sum / count;
                String formattedAverage = String.format("%.2f", average);
                System.out.println("平均价格: " + formattedAverage + "元");
                appendAverageToFile(file, formattedAverage); // 将平均值追加到原文件
            } else {
                System.out.println("没有读取到任何价格数据。");
            }
        } catch (FileNotFoundException e) {
            System.err.println("文件未找到: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("发生错误: " + e.getMessage());
        } finally {
            if (sc != null) {
                sc.close();
            }
        }
    }

    // 追加平均价格到文件
    private static void appendAverageToFile(File file, String average) {
        try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"))) {
            writer.println(); // 添加换行符
            writer.println("平均价格，" + average + "元/件");
        } catch (IOException e) {
            System.err.println("追加平均价格时发生错误: " + e.getMessage());
        }
    }
}
  